package com.scrum.Object;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.mysql.jdbc.Statement;
import com.scrum.Connection.DBConnection;

public class User {
	int id;
	String username;
	String password;
	String image;
	String fullname;
	String role;
	String gender;
	String address;
	String email;

	public User() {
		super();
	}

	public User(int id, String username, String password, String image, String fullname,
			String role, String gender, String address,
			String email) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.image = image;
		this.fullname = fullname;
		this.role = role;
		this.gender = gender;
		this.address = address;
		this.email = email;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getFullname() {
		return fullname;
	}

	public void setFulname(String fullname) {
		this.fullname = fullname;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public static User login(String username, String password) {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		String sql = "Select * from user where username = ? AND password = ?";
		User user = null;
		try {
			con = DBConnection.connectDB();
			pstm = con.prepareStatement(sql);
			pstm.setString(1, username);
			pstm.setString(2, password);
			rs = pstm.executeQuery();
			if (rs.next()) {
				user = new User(rs.getInt("id"), rs.getString("username"),
						rs.getString("password"), rs.getString("image"), rs.getString("fullname"),
						rs.getString("role"), rs.getString("gender"), rs.getString("address"),
						rs.getString("email"));
			}
		} catch (ClassNotFoundException | SQLException e) {

			e.printStackTrace();
		}
		return user;
	}
//
//	public static boolean check(String username) throws ClassNotFoundException,
//			SQLException {
//		Connection con = null;
//		PreparedStatement pstmt = null;
//		ResultSet rs = null;
//		String sql = "select * from user where username = ?";
//		con = DBConnection.connectDB();
//		pstmt = con.prepareStatement(sql);
//		pstmt.setString(1, username);
//		rs = pstmt.executeQuery();
//		if (!rs.next()) {
//			return true;
//		} else
//			return false;
//	}
//
//	public static User register(String username, String password, String name,
//			String role, String gender, String phone, String address,
//			String email) throws ClassNotFoundException, SQLException {
//		User user = null;
//		if (!check(username)) return null;
//		else {
//		Connection con = null;
//		PreparedStatement pstmt = null;
//		ResultSet rs = null;
//		try {
//			String sql = "insert into user(username, password,name,role,gender,phone,address,email) values (?,?,?,?,?,?,?,?)";
//			con = DBConnection.connectDB();
//			pstmt = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
//			pstmt.setString(1, username);
//			pstmt.setString(2, password);
//			pstmt.setString(3, name);
//			pstmt.setString(4, role);
//			pstmt.setString(5, gender);
//			pstmt.setString(6, phone);
//			pstmt.setString(7, address);
//			pstmt.setString(8, email);
//			pstmt.executeUpdate();
//			rs = pstmt.getGeneratedKeys();
//							if (rs != null && rs.next()) {
//					user = new User(rs.getInt(1), username, password, name,
//							role, gender, phone, address, email);
//				}
//		
//		} finally {
//			try {
//				rs.close();
//				pstmt.close();
//				con.close();
//			} catch (Exception ex) {
//			}
//		}
//		return user;
//	}
//	}
//	
	public User update(String username, String password, String image, String fullname, String gender, String address,	String email) throws ClassNotFoundException, SQLException{
		Connection con = null;
        PreparedStatement pstmt = null;

        String sql = "Update user set username =  ?, password = ?,image=?, fullname = ?,  gender = ?,  address = ?,	email = ? where id = ?";
        try {
            con = DBConnection.connectDB();
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            pstmt.setString(3, image);
            pstmt.setString(4, fullname);
            pstmt.setString(5, gender);
            pstmt.setString(6, address);
            pstmt.setString(7, email);
            pstmt.setInt(8, id);
            pstmt.executeUpdate();
        } finally {
            try {
                pstmt.close();
                con.close();
            } catch (Exception ex) {
            }
        }
        return new User(id, username, password, image, fullname, role, gender,  address, email);
    } 
	
//	public static Vector<User> getAllUsers() throws ClassNotFoundException, SQLException{
//		Vector<User> users = new Vector<>();
//		Connection con = null;
//		PreparedStatement pstmt = null;
//		ResultSet rs = null;
//		try {
//			String sql = "select * from user where role = ?";
//			con = DBConnection.connectDB();
//			pstmt = con.prepareStatement(sql);
//			pstmt.setString(1, "User");
//			rs = pstmt.executeQuery();
//							while (rs.next()) {
//					users.add(new User(rs.getInt("id"), rs.getString("username"),
//							rs.getString("password"), rs.getString("name"),
//							rs.getString("role"), rs.getString("gender"),
//							rs.getString("phone"), rs.getString("address"),
//							rs.getString("email")));
//				}
//		
//		} finally {
//			try {
//				rs.close();
//				pstmt.close();
//				con.close();
//			} catch (Exception ex) {
//			}
//		}
//		return users;
//	}
//	
//	public static int getcountUsers() throws ClassNotFoundException, SQLException{
//		int countuser = 0;
//        Connection con = null;
//        PreparedStatement pstmt = null;
//        ResultSet rs = null;
//        try {
//            String sql = "SELECT COUNT(*) AS NumberOfUsers FROM user where role = ?";
//            con = DBConnection.connectDB();
//            pstmt = con.prepareStatement(sql);
//            pstmt.setString(1, "User");
//            rs = pstmt.executeQuery();
//            if (rs.next()) {
//            	countuser = rs.getInt("NumberOfUsers");
//            }
//        }
//        finally {
//                           rs.close();
//                pstmt.close();
//                con.close();
//        }
//        return countuser;
//	}
//
	public static User getUserById(int id) throws ClassNotFoundException, SQLException{
		User user = null;
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            String sql = "SELECT * FROM user where id = ?";
            con = DBConnection.connectDB();
            pstmt = con.prepareStatement(sql);
            pstmt.setInt(1, id);
            rs = pstmt.executeQuery();
            if (rs.next()) {
            	user = new User(rs.getInt("id"), rs.getString("username"),
						rs.getString("password"), rs.getString("image"), rs.getString("fullname"),
						rs.getString("role"), rs.getString("gender"), rs.getString("address"),
						rs.getString("email"));
            }
        }
        finally {
                           rs.close();
                pstmt.close();
                con.close();
        }
        return user;
	}
//	
//	public void delete() throws ClassNotFoundException, SQLException{
//	        Connection con = null;
//	        PreparedStatement pstmt = null;
//	        System.out.println(id);
//	        String sql = "Delete from user where id = ?";
//	        try {
//	            con = DBConnection.connectDB();
//	            pstmt = con.prepareStatement(sql);
//	            pstmt.setInt(1,id);
//	            pstmt.executeUpdate();
//	        } finally {
//	            try {
//	                pstmt.close();
//	                con.close();
//	            } catch (Exception ex) {
//	            }
//	        }
//	}
//	public static Vector<User> getUserByName(String username) throws ClassNotFoundException, SQLException{
//		Connection con = null;
//		PreparedStatement pstmt = null;
//		ResultSet rs = null;
//		Vector<User> users = new Vector<User>();
//		try {
//			String sql = "SELECT * FROM user where username like ?";
//			con = DBConnection.connectDB();
//			pstmt = con.prepareStatement(sql);
//			pstmt.setString(1, "%"+username+"%");
//			rs = pstmt.executeQuery();
//			while (rs.next()) {
//				users.add(new User(rs.getInt("id"), rs.getString("username"),
//						rs.getString("password"), rs.getString("name"),
//						rs.getString("role"), rs.getString("gender"),
//						rs.getString("phone"), rs.getString("address"),
//						rs.getString("email")));
//			}
//		}
//		finally {
//			rs.close();
//			pstmt.close();
//			con.close();
//		}
//		return users;
//		
//	}
}
